package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Constant;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

/**
 * Created by sxy on 2017/11/15.
 */

public class MyIntegralActivity extends BaseActivity implements View.OnClickListener ,SwipeRefreshLayout.OnRefreshListener{

    // Content View Elements
    private android.support.v4.widget.SwipeRefreshLayout swipe_ly;
    private ImageView iv_back;
    private TextView tv_detail;
    private TextView tv_integral;
    private ImageView iv_details;
    // End Of Content View Elements

    private void bindViews() {
        swipe_ly = (android.support.v4.widget.SwipeRefreshLayout) findViewById(R.id.swipe_ly);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_detail = (TextView) findViewById(R.id.tv_detail);
        tv_integral = (TextView) findViewById(R.id.tv_integral);
        iv_details = (ImageView) findViewById(R.id.iv_details);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_integral);
        bindViews();
        iv_back.setOnClickListener(this);
        tv_detail.setOnClickListener(this);
        swipe_ly.setOnRefreshListener(this);
        swipe_ly.setColorSchemeResources(R.color.bg_common_red, R.color.bg_common_red,
                R.color.bg_common_red, R.color.bg_common_red);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipe_ly.setDistanceToTriggerSync(200);
    }

    @Override
    protected void initData() {
        getIntegral();
        DisplayImageOptions options=new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.NONE).build();
        ImageLoader.getInstance().displayImage(Constant.URL_GET_INTEGRAL_IMAGE, iv_details,options);
    }

    private void getIntegral() {
        if (!mDialog.isShowing())
            mDialog.show();
        PersonalNetWork.getIntegral(UserTokenUtil.getToken(MyIntegralActivity.this), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                if (mDialog.isShowing()) {
                    mDialog.dismiss();
                }
                if (swipe_ly.isRefreshing()){
                    swipe_ly.setRefreshing(false);
                }
                if (msg != null && msg.getSuccess() && msg.getData() != null) {
                    String integral=msg.getData()+"";
                    tv_integral.setText(integral);
                }
            }

            @Override
            public void onCallBackError(Msg msg) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                back();
                break;
            case R.id.tv_detail:
                intent = new Intent(this, IntegralListActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh() {
        getIntegral();
    }
}
